import matplotlib.pyplot as plt
from datetime import date
import os

day_permonth = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]


def show_template(template_dictionary, province):
    # 日期处理(自变量)
    dates = ["今天", "明天", "后天"]
    today = date.today()
    month = today.month
    day = today.day
    # 第4、5、6、7天的月份-日期
    day += 2
    for i in range(4):
        day = day + 1 if day + 1 < day_permonth[month] else 1
        if today.day > day:
            month = (month + 1) % 12 if (month + 1) % 12 != 0 else 12
        dates.append(str(month) + "月" + str(day) + "日")
    print(dates)
    # save_path = "../weather-crawler/src/assets/"
    # 温度处理(因变量)
    # save_path = "../"
    cities = list(template_dictionary.keys())
    city_max_min = template_dictionary.values()
    max_list = []
    min_list = []
    for per_city in city_max_min:
        per_day_max = []
        per_day_min = []
        for per_day_max_min in per_city:
            per_day_max.append(int(per_day_max_min[0].replace("℃", "")))
            per_day_min.append(int(per_day_max_min[1].replace("℃", "")))
        max_list.append(per_day_max)
        min_list.append(per_day_min)

    # 图像配置
    plt.rcParams["font.sans-serif"] = ["SimHei"]
    plt.rcParams["axes.unicode_minus"] = False

    # 作图
    fig = plt.figure()

    folder_name = 'pic/' + province
    folder_path = os.path.join("I:/Code_Src/PyCharm-Py/weather-crawler/src/assets", folder_name)
    print(folder_path)
    if not os.path.exists(folder_path):
        os.makedirs(folder_path)
    img_url_dict = {}
    # 按城市遍历
    for city, max_template, min_template in zip(cities, max_list, min_list):
        plt.cla()
        plt.plot(dates, max_template, label='最大温度', linewidth=3, marker='o', markerfacecolor='red')
        plt.plot(dates, min_template, label='最小温度', linewidth=3, marker='o', markerfacecolor='blue')
        plt.title(city + "七日温度")
        plt.xlabel("日期")
        plt.ylabel("温度")

        # 坐标轴
        ax = plt.gca()
        ax.spines['right'].set_visible(False)
        ax.spines['top'].set_visible(False)

        # 设置max标签
        for x, y in zip(dates, max_template):
            plt.text(x, y, y, ha='center', va='top')
        # 设置min标签
        for x, y in zip(dates, min_template):
            plt.text(x, y, y, ha='center', va='top')

        filename = city + '.png'
        plt.savefig(folder_path+"/"+filename)
        img_url_dict[city] = "./src/assets/pic/"+province+"/"+filename
    return img_url_dict
